package eu.sudparis.it.opp.format;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class MacContactClustering {
	public static HashMap<String, Integer> macs = new HashMap<String, Integer>();
	public static HashMap<String, Contact> last = new HashMap<String, Contact>();
	public static HashMap<String, List<Contact>> ctc=new HashMap<String, List<Contact>>();
	public static SimpleDateFormat sdf = new SimpleDateFormat(
			"dd-MMM-yyyy HH:mm:ss", Locale.US);
	public static PrintStream ps = null;
	static {
		try {
			ps = new PrintStream("G://reality_set/contact.dat");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static boolean isNumeric(String str) {
		for (int i = str.length(); --i >= 0;) {
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	public static void main(String[] args) {
		try {
			BufferedReader br = new BufferedReader(new FileReader(new File(
					args[0])));
			String line = br.readLine();
			while (line != null && !line.isEmpty()) {
				String[] lns = line.split("\t");
				if (isNumeric(lns[0]) && !lns[1].isEmpty()) {
					macs.put(lns[1], Integer.parseInt(lns[0]));
				}
				line = br.readLine();
			}
			br = new BufferedReader(new FileReader(new File(args[1])));
			line = br.readLine();
			while (line != null && !line.isEmpty()) {
				String[] lns = line.split("\t");
				if (isNumeric(lns[0])
						&& macs.values().contains(Integer.parseInt(lns[0]))
						&& macs.containsKey(lns[1])
						&& lns[2].length()==20) {
					int er = Integer.parseInt(lns[0]);
					int ee = macs.get(lns[1]);
					long timestamp = sdf.parse(lns[2]).getTime();
					String key = er + "\t" + ee;
					if (!last.containsKey(key)) {
						Contact con = new Contact();
						con.begin = timestamp;
						con.end = timestamp;
						last.put(key, con);
					} else {
						Contact con = last.get(key);
						if (timestamp - con.end <= 1000 * 60 * 8) {//tracking
							con.end = timestamp; //tracking
						} else {
							if(!ctc.containsKey(key)){
								ctc.put(key, new ArrayList<Contact>());
							}
							Contact ct=new Contact();
							ct.begin=con.begin;
							ct.end=con.end;
							ctc.get(key).add(ct);
							con.begin=timestamp;
							con.end=timestamp;
						}
					}
				}
				line = br.readLine();
			}

			for(String key:ctc.keySet()){
				List<Contact> cts=new ArrayList<Contact>(ctc.get(key));
				Collections.sort(cts);
				for(Contact ct:cts){
					ps.println(key+"\t"+(ct.begin/1000)+"\t"+(ct.end/1000));
				}
			}
			ps.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
